package user;

import javax.servlet.*;
import javax.servlet.http.*;

import com.mysql.jdbc.*;

import java.io.*;
import java.sql.SQLException;

public class FoodSearching extends HttpServlet {

	private static ResultSet FoodInfo;

	public static ResultSet getFoodSearchingInfo()
	{
		return FoodInfo;
	}

	/**
	 * Process the HTTP Post request
	 * 
	 * @param request
	 *            an object holding the information sent by the client
	 * @param response
	 *            an object holding the information sent to the client
	 * @throws ServletException
	 *             , IOException
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println("FoodSearching1");
		response.setContentType("text/html");		
		String url = "../search_result.jsp";
		String resultURL = "<script type =\"text/javascript\"> window.location.href = '"  + url + "'</script>";
		PrintWriter out = response.getWriter();		
		
		// Obtain parameter from the client
		String PriceFrom = request.getParameter("price_search_from");
		String PriceTo = request.getParameter("price_search_to");
		String KeywordInputString = request.getParameter("keyword_search");
		String RestaurantInputID = request.getParameter("select_restaurant");
		
		System.out.println("Look at here : " + RestaurantInputID);
		try {
			FoodInfo = findFoodInfo(RestaurantInputID, PriceFrom, PriceTo, KeywordInputString);
			out.write(resultURL);

		} catch (Exception ex) {
			System.out.println("FoodSearching.java Exception");
		} finally {
			out.close(); // Close stream
		}
	}

	// TODO change!!!
	private ResultSet findFoodInfo(String RestaurantInputID, String PriceFrom, String PriceTo, String KeywordInputString) throws SQLException {
		
		String SQLstatement = "select distinct food.food_name, food.food_price, food.food_description, food.food_picture_link, food.food_ID " +
		"from food, restaurant where restaurant.rest_ID = food.rest_ID"; 
		
		String DefaultPriceFrom = "from";
		String defaultPriceTo = "to";
		String DefaultKeyword = "search for keywords";
		String DefaultRestaurantID = "0";
		String search = SQLstatement;

		if (RestaurantInputID.compareTo(DefaultRestaurantID)!=0)
		{
			search = search + " and food.rest_ID = " + RestaurantInputID;
		}
		if (PriceFrom.compareTo(DefaultPriceFrom)!=0 && PriceFrom.length() != 0)
		{
			search = search + " and food_price >= " + Double.parseDouble(PriceFrom);	
		}
		if (PriceTo.compareTo(defaultPriceTo)!=0 && PriceTo.length() != 0)
		{
			search = search + " and food_price <= " + Double.parseDouble(PriceTo);	
		}
		if (KeywordInputString.compareTo(DefaultKeyword)!= 0 && KeywordInputString.length() != 0)
		{
			search = search + " and food_name like '%" + KeywordInputString + "%' or food_description like '%" + KeywordInputString + "%'";
		}
		
		System.out.println(search);
		
		DBConnector db = DBConnector.getDBConnector();
		System.out.println(search);
		db.prepareStatement(search);
    	return db.queryPstmt();
	}
}
